package com.ilop.sthome.ui.adapter.device;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.common.utils.LiveDataBus;
import com.ilop.sthome.common.CommonId;
import com.ilop.sthome.data.greenDao.DeviceShareBean;
import com.siterwell.familywellplus.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author skygge
 * @date 2019/9/27.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：设备分享页面
 */

public class DeviceShareAdapter extends RecyclerView.Adapter<DeviceShareAdapter.ItemHolder> {

    private Context mContext;
    private List<DeviceShareBean> mList;

    public DeviceShareAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void setList(List<DeviceShareBean> mList) {
        this.mList = mList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ItemHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View mView = LayoutInflater.from(mContext).inflate(R.layout.item_device_share, viewGroup, false);
        return new ItemHolder(mView);
    }

    @Override
    public void onBindViewHolder(@NonNull ItemHolder itemHolder, int i) {
        DeviceShareBean shareBean = mList.get(i);
        itemHolder.tvName.setText(shareBean.getIsReceiver() == 0 ? shareBean.getReceiverAlias() : shareBean.getInitiatorAlias());
        itemHolder.tvCancel.setText(shareBean.getIsReceiver() == 0 ? mContext.getString(R.string.cancel) : mContext.getString(R.string.accept));
        if (shareBean.getStatus() == -1 ){
            itemHolder.tvCancel.setTextColor(ContextCompat.getColor(mContext, R.color.main_color));
            itemHolder.tvCancel.setBackgroundResource(R.drawable.btn_empty_bg);
        }else if (shareBean.getStatus() == 0 && shareBean.getIsReceiver() == 0){
            itemHolder.tvCancel.setTextColor(ContextCompat.getColor(mContext, R.color.main_color));
            itemHolder.tvCancel.setBackgroundResource(R.drawable.btn_empty_bg);
        }else {
            itemHolder.tvCancel.setTextColor(ContextCompat.getColor(mContext, R.color.text_normal));
            itemHolder.tvCancel.setBackgroundResource(R.drawable.btn_normal_bg);
        }
        itemHolder.tvStatus.setText(shareBean.getDescription());
        itemHolder.tvCancel.setOnClickListener(v -> {
            if (shareBean.getIsReceiver() == 0){ //分享者
                if (shareBean.getStatus() == -1){ //是否是初始化状态
                    LiveDataBus.get().with(CommonId.SHARE_CANCEL).setValue(shareBean.getRecordId());
                }else if (shareBean.getStatus() == 0){
                    LiveDataBus.get().with(CommonId.SHARE_DELETE).setValue(shareBean);
                }
            }else { //被分享者
                if (shareBean.getStatus() == -1){ //是否是初始化状态
                    LiveDataBus.get().with(CommonId.SHARE_CONFIRM).setValue(shareBean.getRecordId());
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    static class ItemHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_status)
        TextView tvStatus;
        @BindView(R.id.tv_delete_sharer)
        TextView tvCancel;
        public ItemHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
